package io.wit.modules.report;

import io.wit.common.utils.R;
import io.wit.modules.sys.entity.SysDeptEntity;
import io.wit.modules.sys.entity.SysUserEntity;
import io.wit.modules.sys.service.SysDeptService;
import io.wit.modules.wit.entity.FarmEntity;
import io.wit.modules.wit.entity.JsonUtil;
import io.wit.modules.wit.entity.ReportEntity;
import io.wit.modules.wit.entity.SubjectEntity;
import io.wit.modules.wit.service.ReportService;
import io.wit.modules.wit.service.SubjectService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report")
public class ReportController {
	@Autowired
    private ReportService report;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SubjectService subjectService;
	@ResponseBody
	@RequestMapping("/cashierReport")
	public R cashierReport() {
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getRoleId()==6) {//经营主体
			List<ReportEntity> value = report.cashierLists(user.getSubjectId(),0);//支出
			List<ReportEntity> text = report.cashierLists(user.getSubjectId(),1);//收入
			JsonUtil unit=new JsonUtil(value,text);
			return R.ok().put("data", unit);
        }
		else {
			return R.ok().put("data", null);
		}
	}
	@ResponseBody
	@RequestMapping("/estatedistributeReport")
	public R estatedistributeReport() {
		List<ReportEntity> value = report.estatedistributeLists(44);//种值
		JsonUtil unit=new JsonUtil(value,null);
		return R.ok().put("data", unit);
	}
	@ResponseBody
	@RequestMapping("/estatedistributesReport")
	public R estatedistributesReport() {
		List<ReportEntity> value = report.estatedistributeLists(43);//养殖
		JsonUtil unit=new JsonUtil(value,null);
		return R.ok().put("data", unit);
	}
	@ResponseBody
	@RequestMapping("/marketReport")
	public R marketReport() {
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getRoleId()==6) {//经营主体
			List<ReportEntity> value = report.marketLists(user.getSubjectId());
			JsonUtil unit=new JsonUtil(value,null);
			return R.ok().put("data", unit);
        }
		else {
			return R.ok().put("data", null);
		}
	}
	@ResponseBody
	@RequestMapping("/subjectdistributeReport")
	public R subjectdistributeReport() {
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getRoleId()==2||user.getRoleId()==4||user.getRoleId()==5) {
			List<ReportEntity> value = report.subjectdistributeLists(user.getDeptId());
			JsonUtil unit=new JsonUtil(value,null);
			return R.ok().put("data", unit);
        }
		else {
			return R.ok().put("data", null);
		}
	}
	@ResponseBody
	@RequestMapping("/subjectworkerReport")
	public R subjectworkerReport() {
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getRoleId()==6) {//经营主体
			List<ReportEntity> value = report.subjectworkerLists(user.getSubjectId());
			JsonUtil unit=new JsonUtil(value,null);
			return R.ok().put("data", unit);
        }
		else {
			return R.ok().put("data", null);
		}
	}
	/**
	 * 获取机构
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDept")
	public R getDept(Long dataId) {
		if(dataId==-1) {
			SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
			dataId=user.getDeptId();
		}
		List<SysDeptEntity> dept = sysDeptService.getDept(dataId);
		return R.ok().put("dept", dept);
	}
	@RequestMapping("/subjectlist")
	public R subjectlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getDeptId()!=1) {
			params.put("key", user.getDeptId());
		}
		List<SubjectEntity> _list = subjectService.subjectlist(params);
		return R.ok().put("page", _list);
	}
	@RequestMapping("/seedlist")
	public R seedlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getDeptId()!=1) {
			params.put("key", user.getDeptId());
		}
		List<FarmEntity> _list = report.seedlist(params);
		return R.ok().put("page", _list);
	}
	@RequestMapping("/breedlist")
	public R breedlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		SysUserEntity user=(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		if(user.getDeptId()!=1) {
			params.put("key", user.getDeptId());
		}
		List<FarmEntity> _list = report.breedlist(params);
		return R.ok().put("page", _list);
	}
}
